package security;

import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;

public class JavaDB extends JFrame {

	private static final long serialVersionUID = 1L;
	static final String jdb = null;
	private static Connection connect;
	public static String dbUrl = "jdbc:mysql://localhost:3306/keithdb";
	@SuppressWarnings("unused")
	private static String tableName = "userTable";
	private static JLabel welcomeLabel;
	private static JTextField usernameField;
	private static JPasswordField passwordField;
	private static JLabel usernameLabel;
	private static JLabel passwordLabel;
	private static JButton submit, forgotButton;;
	private static JLabel welcomeLabel1;
	private static JTextField usernameField1;
	private static JPasswordField passwordField1;
	private static JLabel usernameLabel1;
	private static JLabel passwordLabel1;
	private static JButton submit1;
	private static ResultSet res;
	private static JavaDB jdb2 = new JavaDB();
    static String driver = "com.mysql.jdbc.Driver";
	private static String user = "root";
	private static String password = "root";

	public static void main(String args[]) throws SQLException,
			InterruptedException {
		jdb2.setVisible(false);
		JavaDB window = new JavaDB("");
		window.addWindowListener(new WindowAdapter() {

			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		createConnection();
		try {
			connect = DriverManager.getConnection(dbUrl, user, password);
		} catch (SQLException XJ015) {

		}
		/**
		 * This part tells the program if the user has an account
		 */
		boolean firstTime = firstTime();
		if (firstTime == false) {
			jdb2.setVisible(true);
		}
		if (firstTime) {
			JavaDB db = new JavaDB("");
			db.createAccount();
		}
	}

	public JavaDB(String str) {
		super("Please type in your username and password");
		Container c = getContentPane();
		c.setLayout(new FlowLayout());
		welcomeLabel1 = new JLabel(
				"Please type in your username and password:\n");
		c.add(welcomeLabel1);
		usernameLabel1 = new JLabel("Username:");
		c.add(usernameLabel1);
		usernameField1 = new JTextField(20);
		c.add(usernameField1);
		passwordLabel1 = new JLabel("\nPassword:");
		c.add(passwordLabel1);
		passwordField1 = new JPasswordField(20);
		c.add(passwordField1);
		submit1 = new JButton("Submit");
		c.add(submit1);
		setSize(325, 200);
		setVisible(false);
	}

	public JavaDB() {
		/**
		 * creates the window
		 */
		super("Please type in your username and password");
		Container c = getContentPane();
		c.setLayout(new FlowLayout());
		welcomeLabel = new JLabel(
				"Please type in your username and password:\n");
		c.add(welcomeLabel);
		usernameLabel = new JLabel("Username:");
		c.add(usernameLabel);
		usernameField = new JTextField(20);
		c.add(usernameField);
		passwordLabel = new JLabel("\nPassword:");
		c.add(passwordLabel);
		passwordField = new JPasswordField(20);
		c.add(passwordField);
		submit = new JButton("Submit");
		c.add(submit);
		ButtonHandler handler = new ButtonHandler();
		submit.addActionListener(handler);
		forgotButton = new JButton("Forgot Password?");
		c.add(forgotButton);
		forgotButton.addActionListener(handler);

		setSize(325, 200);
		setVisible(true);
	}

	private static void createConnection() {

		try {
			try {
				Class.forName(driver).newInstance();
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}

		} catch (ClassNotFoundException except) {
			except.printStackTrace();
			System.exit(0);
		}
	}

	public static boolean firstTime() {
		int userBoolean = JOptionPane
				.showConfirmDialog(
						null,
						" Welcome to the Ceasar Cipher Program! "
								+ "A username and password are required to login.\n To "
								+ "create a new account, press yes. To go to the login screen"
								+ ", press no.\n To end the program, press cancel.",
						"Ceasar Cipher Login", JOptionPane.YES_NO_CANCEL_OPTION);
		if (userBoolean == JOptionPane.YES_OPTION) {
			return true;
		} else if (userBoolean == JOptionPane.CLOSED_OPTION) {
			System.exit(0);
			return false;
		} else if (userBoolean == JOptionPane.CANCEL_OPTION) {
			System.exit(0);
			return false;
		} else {
			return false;
		}

	}

	public void createAccount() {
		NewAccount account = new NewAccount();
		account.action();
	}

	@SuppressWarnings("deprecation")
	public void UserInput() throws SQLException {
		try {
			Statement stmnt = connect.createStatement(
					ResultSet.TYPE_SCROLL_INSENSITIVE,
					ResultSet.CONCUR_READ_ONLY);
			stmnt.execute("use keithdb");
			res = stmnt.executeQuery("select * from userTable");
			while (res.next()) {
				if (res.getString("Username").equalsIgnoreCase(
						usernameField.getText())) {
					if (res.getString("Password").equalsIgnoreCase(
							passwordField.getText())) {
						JOptionPane.showMessageDialog(null, "Correct",
								"Correct", JOptionPane.INFORMATION_MESSAGE);
						CeaserCipher.main();
						this.setVisible(false);
						stmnt.close();
						return;
					}
				}
			}
			if (res.next() == false) {
				JOptionPane.showMessageDialog(null, "Error. Wrong information",
						"Wrong", JOptionPane.ERROR_MESSAGE);
				stmnt.close();
			}
		} catch (SQLException e) {

			Statement stmnt = connect.createStatement();
			stmnt.execute("Create Table userTable"
					+ "(Username varchar(25),Password varchar(25))");
			UserInput();
			stmnt.close();
		}

	}

	public class ButtonHandler implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getSource().equals(submit)) {
				try {
					UserInput();
				} catch (SQLException exi) {
				}
			} else if (e.getSource().equals(forgotButton)) {
				jdb2.setVisible(false);
				SendMailSSL.create();

			}
		}

	}
}
